package com.yonyou.crm.sprc.oppdimension.ctrl;

import com.yonyou.crm.base.doc.rmitf.IBaseDocRmService;
import com.yonyou.crm.common.exception.CrmMessageException;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.common.rest.constant.HttpKeyConstant;
import com.yonyou.crm.common.rest.constant.PCCodeConstant;
import com.yonyou.crm.common.rest.util.RequestUtil;
import com.yonyou.crm.common.rest.util.ResponseUtil;
import com.yonyou.crm.sprc.oppdimension.entity.OppdimensionVO;
import com.yonyou.crm.sprc.oppdimension.rmitf.IOppdimensionRmService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/sprc/oppdimensions")
public class OppdimensionCtrl {

	@Autowired
	private IOppdimensionRmService service;


	/**
	 * 商机维度列表
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException 
	 */
	@RequestMapping(method = RequestMethod.GET)
	public Object list(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);

		@SuppressWarnings("unchecked")
		Page<OppdimensionVO> page = service.getList(getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page);
		
	}


	/**
	 * 获取全部商机维度
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "all",method = RequestMethod.GET)
	public Object getAll(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		@SuppressWarnings("unchecked")
		List<OppdimensionVO> voList = service.getAll();
		return ResponseUtil.getObject(getListData(voList));
	}

	/**
	 * 获取全部商机维度
	 * @param request
	 * @param response
	 * @return
	 * @throws CrmMessageException
	 */
	@RequestMapping(value = "enum",method = RequestMethod.GET)
	public Object getEnum(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {
		JSONObject paramObj = RequestUtil.getParamObject(request);
		@SuppressWarnings("unchecked")
		List<OppdimensionVO> voList = service.getAll();
		List<Map<String,String>> resultList = new ArrayList<Map<String,String>>();
		for(int i=0;i<voList.size();i++){
			Map<String,String> map = new HashMap<String, String>();
			map.put("key",voList.get(i).getId().toString());
			map.put("title",voList.get(i).getName());
			resultList.add(map);
		}
		JSONObject result = new JSONObject();
		result.put("dimension",resultList);
		return ResponseUtil.getObject(result);
	}


	private Map<String, Object> getListData(List<OppdimensionVO> voList) {

		if (voList == null) {
			voList = new ArrayList<OppdimensionVO>();
		}

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("voList", voList);

		return data;
	}
	/**
	 * 新增商机维度
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Object insert(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		OppdimensionVO vo = RequestUtil.getVO(request, OppdimensionVO.class);
		
		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		vo = service.insert(vo);
		
		if (vo.getId() == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "获取自增主键为空");
		}
				
		return ResponseUtil.getObject(vo,"插入成功");
	}
	
	/**
	 * 修改商机维度
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.PUT)
	public Object update(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		OppdimensionVO vo = RequestUtil.getVO(request, OppdimensionVO.class);
		if (vo == null) {
        	throw new CrmMessageException(PCCodeConstant.ServerError, "转换实体失败");
		}
		
		vo.setId(id);
		
		String validMsg = validator(vo);
		if (!validMsg.equals("")){
        	throw new CrmMessageException(PCCodeConstant.ServerError, validMsg);
		}
		
		return ResponseUtil.getObject(service.update(vo),"修改成功");
	}


	/**
	 * 批量删除
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "batch", method = RequestMethod.DELETE)
	public Object batchDelete(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}


		@SuppressWarnings("unchecked")
		Page<OppdimensionVO> page = service.batchDelete(idArray,getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));

		return ResponseUtil.getObject(page,"删除成功");
	}

	/**
	 * 批量停启用
	 * @param request
	 * @param response
	 * @return
	 */

	@RequestMapping(value = "state", method = RequestMethod.PUT)
	public Object batchUpdateEnableState(HttpServletRequest request, HttpServletResponse response) throws CrmMessageException {

		JSONObject paramObj = RequestUtil.getParamObject(request);
		String idsStr = paramObj.getString("ids");

		if (idsStr == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "ids参数为空");
		}

		String[] idArray = idsStr.split(",");

		if (idArray.length == 0) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "至少选中一条记录");
		}

		Integer enableState = paramObj.getInt("enableState");

		if (enableState == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "停启用状态不能为空");
		}

		@SuppressWarnings("unchecked")
		Page<OppdimensionVO> page = service.batchUpdateEnableState(idArray, enableState,getRequestPage(paramObj), (Map<String, Object>)paramObj.get("searchMap"));
		String msg="停用成功";
		if("1".equals(enableState.toString())){
			msg="启用成功";
		}
		return ResponseUtil.getObject(page,msg);
	}


	private String validator(OppdimensionVO vo) {
		
		if (vo == null) {
			return "转换实体失败";
		}
		
		if (StringUtils.isBlank(vo.getName())) {
        	return  "名称不能为空";
		}

		return "";
	}
	/**
	 * 获取分页信息
	 * @param paramObj
	 * @return Page<MeasuresVO>
	 */
	private Page<OppdimensionVO> getRequestPage(JSONObject paramObj) throws CrmMessageException {

		Integer page = paramObj.getInt(HttpKeyConstant.Page);
		Integer pageSize = paramObj.getInt(HttpKeyConstant.PageSize);

		if (page == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "page不能为空");
		}

		if (pageSize == null) {
			throw new CrmMessageException(PCCodeConstant.ServerError, "pageSize不能为空");
		}

		return new Page<OppdimensionVO>(page, pageSize);
	}
}
